package com.nexus.service.sys.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nexus.core.exception.ServiceException;
import com.nexus.core.service.TreeService;
import com.nexus.core.sys.entity.SysOrg;
import com.nexus.core.sys.dto.SysOrgDTO;
import com.nexus.core.sys.dto.SysRoleDTO;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.service.sys.mapper.SysOrgMapper;
import com.nexus.service.sys.service.mapstruct.SysOrgWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 组织机构Service
 *
 * @author 路正宁
 * @version 2023-03-15
 */
//@DS("zxhy_platform_db")
//@Service
@Transactional(rollbackFor = {ServiceException.class,Exception.class, RuntimeException.class})
public class SysOrgService extends TreeService<SysOrgMapper, SysOrg, SysOrgDTO,SysOrgWrapper> {

    @Autowired
    private SysOrgUserService sysOrgUserService;
    @Autowired
    private SysPostService sysPostService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleDataruleService sysRoleDataruleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysOfficeService sysOfficeService;

    @Autowired
    SysOrgWrapper sysOrgWrapperImpl;
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SysOrgDTO findById(String id) {
        return baseMapper.findById(id);
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    public List<SysOrgDTO> listDTO() {
        return baseMapper.findList();
    }


    /**
     * 删除机构
     * @Date 2023/9/6 10:09
     * @Author luzhengning
     **/
    public boolean deleteOrgs(List<String> idList){
        //删除组织机构
        //this.removeWithChildrenByIds(idList);
        //删除机构用户关联
        sysOrgUserService.deleteByOrgIds(idList);
        //删除机构职务关联
        sysPostService.deleteByOrgIds(idList);
        //删除机构部门关联
        sysOfficeService.deleteByOrgIds(idList);
        //查询机构下的角色
        IPage<SysRoleDTO> roleList = sysRoleService.findByOrgIds(idList);
        //角色IDs
        List<String> roleIds=new ArrayList<>(16);
        if(ObjectUtils.isNotEmpty(roleList.getRecords())){
            roleIds=roleList.getRecords().stream().map(SysRoleDTO::getId).collect(Collectors.toList());
        }
        //删除机构角色关联
        sysRoleService.deleteByOrgIds(idList);
        //删除角色数据权限关联
        sysRoleDataruleService.deleteByRoleIds(roleIds);
        //删除菜单角色关联
        sysRoleMenuService.deleteByRoleIds(roleIds);
        //删除角色用户关联
        sysUserRoleService.deleteByRoleIds(roleIds);
        return true;
    }

    /**
     * 同步机构数据
     * @return
     */
    public List<SysOrgDTO> syncData() {
        return baseMapper.syncData();
    }

    public SysOrg findByWmsShipperId(String wmsShipperId){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("wms_shipper_id",wmsShipperId);
        queryWrapper.eq("deleted",0);
        List<SysOrg> list = list(queryWrapper);
        if(list!=null&&list.size()>0){
            return list.getFirst();
        }
        return null;
    }

    public SysOrg findByWmsShipperCode(String wmsShipperCode){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("wms_shipper_code",wmsShipperCode);
        queryWrapper.eq("deleted",0);
        List<SysOrg> list = list(queryWrapper);
        if(list!=null&&list.size()>0){
            return list.getFirst();
        }
        return null;
    }
    public SysOrg findByName(String name){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("name",name);
        queryWrapper.eq("deleted",0);
        List<SysOrg> list = list(queryWrapper);
        if(list!=null&&list.size()>0){
            return list.getFirst();
        }
        return null;
    }



}
